<template>
  <div class="app-container">
    <el-card shadow="hover">
      <div>
        <i class="el-icon-tickets"></i>
        <span> 擅长领域</span>
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="small"
          @click="handleAddCate"
          style="margin-left: 20px;"
          >新增</el-button
        >
        <!-- <el-button
          type="danger"
          icon="el-icon-delete"
          size="small"
          :disabled="multiple"
          @click="handleDelete"
          >批量删除</el-button
        > -->
      </div>
      <el-table
        v-loading="loading"
        :data="cateList"
        style="margin-top: 10px;"
      >
        <el-table-column type="index" label="序号" width="70" align="center" />
        <el-table-column
          label="擅长领域"
          prop="name"
          align="center"
          width="200"
        ></el-table-column>
        <el-table-column
          label="标签分类"
          prop="type"
          :formatter="(row,column) => row.type == 1?'擅长领域':row.type == 2?'认证体系':'-'"
          align="center"
          width="200"
        ></el-table-column>
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
          width="200"
        >
          <template slot-scope="scope">
            <el-button
              size="small"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdateCate(scope.row)"
              >修改</el-button
            >
            <el-button
              size="small"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete1(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="newsTotal1 > 0"
        :total="newsTotal1"
        :page.sync="queryParams1.pageNum"
        :limit.sync="queryParams1.pageSize"
        @pagination="getCateList"
      />
    </el-card>

    <el-card shadow="hover" style="margin-top: 20px;">
      <div>
        <i class="el-icon-tickets"></i>
        <span>老师列表</span>
        <el-select class="select" style="margin-right:10px" v-model="queryParams.status" placeholder="请选择审核状态" clearable @change="handleQuery">
          <el-option label="全部" value=""></el-option>
          <el-option label="已通过" value="1"></el-option>
          <el-option label="未通过" value="0"></el-option>
        </el-select>
        <el-input placeholder="请输入老师名称" style="width:200px" v-model="queryParams.name"></el-input>
        <el-button
          icon="el-icon-search"
          type="cyan"
          size="small"
          style="margin-left: 20px;"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="small"
          @click="handleAdd"
          >新增</el-button
        >
        <el-button
          icon="el-icon-circle-close"
          size="small"
          @click="rest"
          >重置</el-button
        >
        <!-- <el-button
          type="danger"
          icon="el-icon-delete"
          size="small"
          :disabled="multipleList"
          @click="handleDeleteNews"
          >批量删除</el-button
        > -->
      </div>
      <el-table
        v-loading="loading1"
        :data="newsList"
        border
        style="margin-top: 10px;"
      >
        <el-table-column type="index" label="序号" width="70" align="center" />
        <el-table-column
          label="老师名称"
          prop="name"
          align="center"
          width="300"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column label="logo" align="center" min-width="160">
          <template slot-scope="scope">
            <img style="height: 80px" :src="showUrl + scope.row.logo" />
          </template>
        </el-table-column>
        <el-table-column
          label="工作年限"
          prop="workYear"
          align="center"
        ></el-table-column>
        <el-table-column show-overflow-tooltip prop="exprience" width="250" label="工作经历" align="center" > </el-table-column>
        <el-table-column  prop="sort" label="排序值" align="center" > </el-table-column>
        <!-- <el-table-column
          label="文章内容"
          prop="consult"
          align="center"
          width="200"
          show-overflow-tooltip
        ></el-table-column> -->
        <el-table-column
          label="操作"
          align="center"
          min-width="200"
          fixed="right"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              >修改</el-button
            >
            <el-button
              size="mini"
              type="danger"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="newsTotal > 0"
        :total="newsTotal"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>

    <!-- 添加/编辑 政策咨询分类 -->
    <el-dialog :title="cateTitle" :visible.sync="cateOpen" width="600px">
      <el-form
        ref="cateForm"
        :model="cateForm"
        :rules="cateRules"
        label-width="120px"
      >
        <el-form-item label="分类名称：" prop="name">
          <el-input v-model="cateForm.name"></el-input>
        </el-form-item>
        <el-form-item label="标签分类：" >
            <el-radio-group v-model="cateForm.type">
                <el-radio :label="1">擅长领域</el-radio>
                <el-radio :label="2">认证体系</el-radio>
            </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitCateForm">确 定</el-button>
        <el-button @click="cancel1">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改 资讯文章 -->
    <el-dialog :title="title" :visible.sync="open" width="65%">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="老师名称:" prop="name">
              <el-input v-model="form.name" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="老师电话:">
              <el-input v-model="form.phone" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="老师email:">
              <el-input v-model="form.email" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="擅长领域:" prop="fieldId">
              <el-select
                v-model="form.fieldId"
                multiple
                placeholder="请选择擅长领域"
              >
                <el-option
                  v-for="item in cateList1"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="认证体系:" prop="classifyId">
              <el-select
                v-model="form.classifyId"
                multiple
                placeholder="请选择认证体系"
              >
                <el-option
                  v-for="item in classList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="工作年限:" prop="workYear">
              <el-input-number v-model="form.workYear" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="工作经历:" prop="exprience">
              <el-input v-model="form.exprience" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="服务感言:" prop="comment">
              <el-input v-model="form.comment" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="审核状态:" prop="comment">
                <el-select v-model="form.status" placeholder="请选择审核状态" >
                    <el-option label="已通过" :value="1"></el-option>
                    <el-option label="未通过" :value="0"></el-option>
                </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item prop="cityIds" label="选择省">
                <el-select multiple v-model="form.cityIds" placeholder="请选择省">
                    <el-option
                    v-for="item in provOp1"
                    :key="item.id"
                    :label="item.cityname"
                    :value="item.id">
                    </el-option>
                </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="排序:" prop="sort">
              <el-input-number v-model="form.sort" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="上传logo:" >
              <el-upload
                class="avatar-uploader"
                :action="upImage + '/cms/company/upload'"
                :show-file-list="false"
                :headers="headers"
                :on-success="handleAvatarSuccess"
              >
                <img
                  v-if="form.logo"
                  :src="showUrl + form.logo"
                  class="avatar"
                />
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="咨询经历:" prop="consult">
              <Editor v-model="form.consult" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitNewsForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  queryNewsCate,
  addNewsCate,
  updateNewsCate,
  delNewsCate,
  queryNews,
  addNews,
  updateNews,
  delNews,
  getclassify,
  upclassify,
  upclassify1,
  getclassify1,
  getclassify2,
  getclassify3,
  delgetlist,
  delgetlist1
} from "@/api/web/power";
import Editor from "@/components/Editor";
import { getToken } from "@/utils/auth";
import {seleCity} from '@/api/web/cer'
import { constants } from 'fs';

export default {
  name: "Information",
  components: {
    Editor
  },
  data() {
    return {
      headers: { Authorization: "Bearer " + getToken() },
      upImage: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      loading1: true,
      // 选中数组
      ids: [],
      all: [],
      // 非单个禁用
      single: true,
      singleList: true,
      // 非多个禁用
      multiple: true,
      multipleList: true,
      // 总条数
      total: 0,
      newsTotal: 0,
      newsTotal1: 0,
      // 政策咨询分类数据
      cateList: [],
      cateList1:[],
      cateTitle: "",
      cateOpen: false,
      cateForm: {},
      cateRules: {
        classifyName: [
          { required: true, message: "资讯百科类名不能为空", trigger: "blur" }
        ]
      },

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        classifyId: '',
      },
       queryParams1: {
        pageNum: 1,
        pageSize: 10,
      },
      newsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      classList:[],
      provOp1:[],
      // 表单校验
      rules: {
        name: [
          { required: true, message: "名称不能为空", trigger: "blur" }
        ],
        classifyId: [
          { required: true, message: "分类不能为空", trigger: "blur" }
        ],
        logo: [
          { required: true, message: "图片不能为空", trigger: "blur" }
        ],
        introduction: [
          { required: true, message: "文章简述不能为空", trigger: "blur" }
        ],
        cityIds:[
          { required: true, message: "请选择省", trigger: "blur" }
        ],
        consult: [
          { required: true, message: "文章内容不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getCateList();
    this.getCateList1();
    this.getCateList2();
    this.getList();
    this.getCity1('',1)
  },
  methods: {
    // 查询政策咨询分类
    getCateList() {
      this.loading = true;
      getclassify(this.queryParams1).then(res => {
        // console.log(res);
        this.cateList = res.rows;
        this.newsTotal1 = res.total;
        this.loading = false;
      });
    },
    // 查询政策咨询分类
    getCateList1() {
        let data = {
            type:1,
            pageNum:1,
            pageSize:1000
        }
      getclassify(data).then(res => {
        // console.log(res);
        this.cateList1 = res.rows;
      });
    },
    // 重置
    rest() {
        this.queryParams = {
            pageNum: 1,
            pageSize: 10,
        }
        this.getList()
    },
    // 查询政策咨询分类
    getCateList2() {
        let data = {
            type:2,
            pageNum:1,
            pageSize:1000
        }
      getclassify(data).then(res => {
        // console.log(res);
        this.classList = res.rows;
      });
    },
    // 点击新增分类
    handleAddCate() {
      // if (this.total < 3) {
        this.cateForm = {};
        this.cateOpen = true;
        this.cateTitle = "新增分类";
      // } else {
      //   return this.msgError("政策咨询分类不能超过3项！");
      // }
    },
    
    // 点击编辑分类
    handleUpdateCate(row) {
      this.cateForm = row;
      this.cateOpen = true;
      this.cateTitle = "修改分类";
    },
    // 提交政策咨询分类
    submitCateForm() {
      this.$refs["cateForm"].validate(valid => {
        if (valid) {
          if (this.cateForm.id != undefined) {
            upclassify1(this.cateForm).then(res => {
              if (res.code == 200) {
                this.msgSuccess("修改成功");
                this.getCateList();
                this.getList();
                this.cateOpen = false;
              }
            });
          } else {
            upclassify(this.cateForm).then(res => {
              if (res.code == 200) {
                this.msgSuccess("保存成功");
                this.getCateList();
                this.getList();
                this.cateOpen = false;
              }
            });
          }
        }
      });
    },
    cancel1() {
      this.cateOpen = false;
      this.getCateList();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    // 删除
    handleDelete(row) {
      const id = row.id;
      this.$confirm("是否确认删除?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return delgetlist(id);
        })
        .then(() => {
          this.getCateList();
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(function() {});
    },
    // 删除
    handleDelete1(row) {
      const id = row.id;
      this.$confirm("是否确认删除?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return delgetlist1(id);
        })
        .then(() => {
          this.getCateList();
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(function() {});
    },
    // 获取省市区
    getCity1(id,type) {
        let region = {
                type:type,
                pid:id
            }
        seleCity(region).then(res => {
            if (res.code == 200) {
                this.provOp1 = res.rows;
            }
        })
    },
    // 查询资讯百科文章
    getList() {
      this.loading1 = true;
      getclassify1(this.queryParams).then(res => {
        console.log(res);
        this.newsList = res.rows;
        this.newsTotal = res.total;
        this.loading1 = false;
      });
    },
    getClassifyName(id) {
      let cate = this.cateList.find(item => {
        return item.id == id;
      });
      if (cate != undefined) {
        return cate.name;
      }
    },
    // 点击新增
    handleAdd() {
      this.form = {
        name: "",
        workYear: 1,
        consult: "",
        exprience: "",
        comment: "",
        course: "",
        sort: 1,
        logo: "",
        fieldId: "",
        genreId: "",
        cityIds:"", //省id
        status:1
      };
      this.open = true;
      this.title = "添加老师";
    },
    handleUpdate(row) {
      this.open = true;
      this.title = "编辑老师";
      this.$nextTick(() => {
      this.form = JSON.parse(JSON.stringify(row));
        if(this.form.cityIds != null||this.form.cityIds != ''){
            this.form.cityIds = this.form.cityIds.split(',')
            this.form.cityIds = this.form.cityIds.map(item => Number(item))
        }else {
            this.form.cityIds = []
        }
        if(this.form.classifyId != null||this.form.classifyId != ''){
            this.form.classifyId = this.form.classifyId.split(',')
            this.form.classifyId = this.form.classifyId.map(item => Number(item))
        }else {
            this.form.classifyId = []
        }
        if(this.form.fieldId != null||this.form.fieldId != ''){
            this.form.fieldId = this.form.fieldId.split(',')
            this.form.fieldId = this.form.fieldId.map(item => Number(item))
        }else {
            this.form.fieldId = []
        }
      })
    },
    handleAvatarSuccess(res) {
      this.form.logo = res.data;
    },
    cancel() {
      this.open = false;
      this.getList();
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    // 提交资讯文章表单信息
    submitNewsForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.cityIds = this.form.cityIds.toString()
          this.form.classifyId = this.form.classifyId.toString()
          this.form.fieldId = this.form.fieldId.toString()
          if (this.form.id != undefined) {
            getclassify3(this.form).then(res => {
              if (res.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            getclassify2(this.form).then(res => {
              if (res.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    handleSelectionChange1(selection) {
      this.all = selection.map(item => item.id);
      this.singleList = selection.length != 1;
      this.multipleList = !selection.length;
      console.log(this.all)
    },
    // handleDeleteNews(row) {
    //   const id = row.id || this.all;
    //   console.log(id)
    //   this.$confirm("是否确认删除?", "警告", {
    //     confirmButtonText: "确定",
    //     cancelButtonText: "取消",
    //     type: "warning"
    //   })
    //     .then(function() {
    //       return delgetlist(id);
    //     })
    //     .then(() => {
    //       this.getList();
    //       this.msgSuccess("删除成功");
    //     })
    //     .catch(function() {});
    // }
  }
};
</script>

<style scoped>
.select {
  margin-left: 20px;
}
.avatar-uploader .el-upload {
  width: 148px;
  height: 148px;
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #20a0ff;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 148px;
  height: 148px;
  line-height: 148px;
  text-align: center;
  border: 1px dashed #ccc;
}

.avatar {
  width: 148px;
  height: 148px;
  display: block;
}
</style>
